{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/00a_RF_tuning_US.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}18 Dec 2023, 15:32:27
{txt}
{com}. 
. ***************************************************************
. ***US***
. ***************************************************************
. 
. set seed 1234
{txt}
{com}. 
. *Prepare data
. use "Dataset.dta", clear
{txt}
{com}. keep if sender=="US"
{txt}(10,154 observations deleted)

{com}. 
. ** Filter for cases of importance
. keep if pot_sanctioned_countries == 1
{txt}(1,296 observations deleted)

{com}. 
. xtset ccodecow year
{res}
{col 1}{txt:Panel variable: }{res:ccodecow}{txt: (unbalanced)}
{p 1 16 2}{txt:Time variable: }{res:year}{txt:, }{res:{bind:1989}}{txt: to }{res:{bind:2015}}{p_end}
{txt}{col 10}Delta: {res}1 unit
{txt}
{com}. gen ln_oil_gas_value_2014 = ln(oil_gas_value_2014+1)
{txt}(197 missing values generated)

{com}. gen sender_colony=US_colony
{txt}(13 missing values generated)

{com}. 
. gen sender_additional=cond(threatEU==1 | impositionEU == 1, 1, 0)
{txt}
{com}. gen only_threat=cond(threatUS==1 & impositionUS == 0, 1, 0)
{txt}
{com}. 
. gen sender_trade = ln_US_Trade_COW
{txt}(234 missing values generated)

{com}. gen coup_dummy = coup1
{txt}(5 missing values generated)

{com}. replace coup_dummy = 0 if coup_dummy == 1
{txt}(61 real changes made)

{com}. replace coup_dummy = 1 if coup_dummy == 2
{txt}(45 real changes made)

{com}. 
. * variable which is 1 if a threat or sanction case was ongoing in the dyad
. gen sanction_threat = sanction_dyad
{txt}
{com}. replace sanction_threat = 1 if threat_dyad==1
{txt}(179 real changes made)

{com}. tab sanction_threat

{txt}sanction_th {c |}
       reat {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          0 {c |}{res}      2,793       73.87       73.87
{txt}          1 {c |}{res}        988       26.13      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      3,781      100.00
{txt}
{com}. gen sanction_train= sanction_threat if year < 2009
{txt}(992 missing values generated)

{com}. gen sanction_test= sanction_threat if year >= 2009
{txt}(2,789 missing values generated)

{com}. 
. * lag time-series variables
. sort ccodecow year
{txt}
{com}. by ccodecow: gen l_v2x_polyarchy = v2x_polyarchy[_n-1] if year==year[_n-1]+1
{txt}(240 missing values generated)

{com}. by ccodecow: gen l_gd_ptss = gd_ptss[_n-1] if year==year[_n-1]+1
{txt}(225 missing values generated)

{com}. by ccodecow: gen l_coup_dummy = coup_dummy[_n-1] if year==year[_n-1]+1
{txt}(151 missing values generated)

{com}. by ccodecow: gen l_one_sided_violence = one_sided_violence[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. by ccodecow: gen l_conflict = conflict[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. by ccodecow: gen l_mid_terr_integrity = mid_terr_integrity[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. by ccodecow: gen l_ln_GDPpc_imputed = ln_GDPpc_imputed[_n-1] if year==year[_n-1]+1
{txt}(187 missing values generated)

{com}. by ccodecow: gen l_sender_trade = sender_trade[_n-1] if year==year[_n-1]+1
{txt}(237 missing values generated)

{com}. by ccodecow: gen l_ln_oil_gas_value = ln_oil_gas_value_2014[_n-1] if year==year[_n-1]+1
{txt}(202 missing values generated)

{com}. by ccodecow: gen l_defense_alliance = defense_alliance[_n-1] if year==year[_n-1]+1
{txt}(147 missing values generated)

{com}. 
. * create dummy variables
. tabulate l_gd_ptss, generate (pol_terr)

  {txt}l_gd_ptss {c |}      Freq.     Percent        Cum.
{hline 12}{c +}{hline 35}
          1 {c |}{res}        465       13.08       13.08
{txt}          2 {c |}{res}      1,009       28.37       41.45
{txt}          3 {c |}{res}      1,190       33.46       74.92
{txt}          4 {c |}{res}        634       17.83       92.74
{txt}          5 {c |}{res}        258        7.26      100.00
{txt}{hline 12}{c +}{hline 35}
      Total {c |}{res}      3,556      100.00
{txt}
{com}. 
. 
. ** Imposition
. * Tuning number of iterations of rf model
. * Randomize data
. gen u=0
{txt}
{com}. replace u=1 if year >= 2009
{txt}(992 real changes made)

{com}. sort u
{txt}
{com}. gen out_of_bag_error1 = .
{txt}(3,781 missing values generated)

{com}. gen training_error = .
{txt}(3,781 missing values generated)

{com}. gen recall_iter = .
{txt}(3,781 missing values generated)

{com}. gen recall_training_sample_iter = .
{txt}(3,781 missing values generated)

{com}. gen iter1 = .
{txt}(3,781 missing values generated)

{com}. local j = 0
{txt}
{com}. 
. forvalues i = 100(200)1500{c -(}
{txt}  2{com}. local j = `j' + 1
{txt}  3{com}. rforest sanction_threat l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/2789, ///
> type(class) iter(`i') numvars(1)
{txt}  4{com}. replace iter1 = `i' in `j'
{txt}  5{com}. replace out_of_bag_error1 = `e(OOB_Error)' in `j'
{txt}  6{com}. predict p in 2790/3781
{txt}  7{com}. count if p==1 & sanction_test == 1
{txt}  8{com}. replace recall_iter = r(N)/203 in `j'
{txt}  9{com}. predict p2 in 1/2789
{txt} 10{com}. count if p2==1 & sanction_train == 1
{txt} 11{com}. replace recall_training_sample_iter = r(N)/785 in `j'
{txt} 12{com}. replace training_error = `e(error_rate)' in `j'
{txt} 13{com}. drop p p2
{txt} 14{com}. {c )-}
{txt}(1 real change made)
(1 real change made)
  {res}93
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}97
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}97
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}97
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)

{com}. 
. *Additional tuning based on recall rate, as outcome variable is imbalanced.
. label var recall_iter "Recall Test Sample"
{txt}
{com}. label var recall_training_sample_iter "Recall Training Sample"
{txt}
{com}. scatter recall_iter recall_training_sample_iter iter1
{res}{txt}
{com}. 
. graph export "Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_iterations_Recall_Training_Test.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_iterations_Recall_Training_Test.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_iterations_Recall_Training_Test.png{rm}
saved as
PNG
format
{p_end}

{com}. 
. * Tuning based on Accuracy
. label var out_of_bag_error1 "Out of Bag Error"
{txt}
{com}. label var training_error "Accuracy Test Sample"
{txt}
{com}. scatter out_of_bag_error1 training_error iter1
{res}{txt}
{com}. graph export "Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_iterations_Accuracy.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_iterations_Accuracy.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_iterations_Accuracy.png{rm}
saved as
PNG
format
{p_end}

{com}. * No benefit to increasing beyond 1500 iterations
. 
. drop out_of_bag_error1 training_error recall_iter recall_training_sample_iter
{txt}
{com}. 
. * Tuning number of variables to include in model
. gen out_of_bag_error1 = .
{txt}(3,781 missing values generated)

{com}. gen training_error = .
{txt}(3,781 missing values generated)

{com}. gen nvars = .
{txt}(3,781 missing values generated)

{com}. gen recall_iter = .
{txt}(3,781 missing values generated)

{com}. gen recall_training_sample_iter = .
{txt}(3,781 missing values generated)

{com}. local j = 0
{txt}
{com}. 
. forvalues i = 1(1)15{c -(}
{txt}  2{com}. local j = `j' + 1
{txt}  3{com}. rforest sanction_threat l_v2x_polyarchy pol_terr* l_coup_dummy ///
> l_one_sided_violence l_conflict l_mid_terr_integrity ///
> l_ln_GDPpc_imputed l_sender_trade l_ln_oil_gas_value ///
> sender_colony l_defense_alliance in 1/2789, ///
> type(class) iter(1500) numvars(`i')
{txt}  4{com}. replace nvars = `i' in `j'
{txt}  5{com}. replace out_of_bag_error1 = `e(OOB_Error)' in `j'
{txt}  6{com}. predict p in 2790/3781
{txt}  7{com}. count if p==1 & sanction_test == 1
{txt}  8{com}. replace recall_iter = r(N)/203 in `j'
{txt}  9{com}. predict p2 in 1/2789
{txt} 10{com}. count if p2==1 & sanction_train == 1
{txt} 11{com}. replace recall_training_sample_iter = r(N)/785 in `j'
{txt} 12{com}. replace training_error = `e(error_rate)' in `j'
{txt} 13{com}. drop p p2
{txt} 14{com}. {c )-}
{txt}(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}99
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}97
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}94
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}95
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}96
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}96
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}95
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}100
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}98
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}101
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)
(1 real change made)
(1 real change made)
  {res}101
{txt}(1 real change made)
  {res}766
{txt}(1 real change made)
(1 real change made)

{com}. 
. *Additional tuning based on recall rate, as outcome variable is imbalanced.
. label var recall_iter "Recall Test Sample"
{txt}
{com}. label var recall_training_sample_iter "Recall Training Sample"
{txt}
{com}. scatter recall_iter recall_training_sample_iter nvars
{res}{txt}
{com}. graph export "Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_numvars_Recall_Training_Test.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_numvars_Recall_Training_Test.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_numvars_Recall_Training_Test.png{rm}
saved as
PNG
format
{p_end}

{com}. 
. * Accuracy
. label var out_of_bag_error1 "Out of Bag Error"
{txt}
{com}. label var training_error "Accuracy Test Sample"
{txt}
{com}. scatter out_of_bag_error1 training_error nvars 
{res}{txt}
{com}. graph export "Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_numvars_Accuracy.png", as(png) name("Graph") replace
{txt}{p 0 4 2}
(file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_numvars_Accuracy.png{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
Supplemental_Material\Random_Forest_Setup\RF_US_tuning_pot_sanctions_numvars_Accuracy.png{rm}
saved as
PNG
format
{p_end}

{com}. * Accuracy decreases until the end, so include all 15 vars.
. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\thies\OneDrive\00_Promotion\00_Output\00_Paper\2022_Predicting_Econ_Sanctions\Empirics\20231214_Replication\Log_files/00a_RF_tuning_US.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}18 Dec 2023, 15:58:46
{txt}{.-}
{smcl}
{txt}{sf}{ul off}